package org.xyf.IM;


import io.grpc.NameResolverRegistry;
import io.micrometer.core.instrument.binder.grpc.ObservationGrpcClientInterceptor;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.xyf.IM.config.GrpcConfig;
import org.xyf.IM.config.SentinelConfig;
import org.xyf.grpcService.HelloReply;
import org.xyf.grpcService.HelloRequest;
import org.xyf.grpcService.UserServiceGrpc;
import reactor.core.publisher.Hooks;

import java.util.List;

@SpringBootApplication
@EnableDiscoveryClient
@Slf4j
public class Main implements ApplicationRunner {

    @Resource
    private UserServiceGrpc.UserServiceBlockingStub userServiceBlockingStub;

    @Resource
    private DiscoveryClient discoveryClient;

    public static void main(String[] args) {
        NameResolverRegistry.getDefaultRegistry().register(new GrpcConfig.CustomNameResolverProvider());
        SentinelConfig.initRules();
        SpringApplication.run(Main.class,args);

    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<ServiceInstance> instances = discoveryClient.getInstances("gateway");
        log.info("服务启动完成; instances is : {}",instances);

//        HelloRequest request = HelloRequest.newBuilder()
//                .setName("xyf")
//                .build();
//        HelloReply reply = userServiceBlockingStub.sayHello(request);
//        log.info("userService grpc响应为: {}",reply);
    }
}